const { DataTypes } = require('sequelize');
// const sequelize = require('@/db/seq');
const LinkCategory = require('./category');
const User = require('../user');
const sequelize = require("@/db/seq");

const Link = sequelize.define('links', {
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '链接标题'
  },
  url: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '链接地址'
  },
  description: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '链接描述'
  },
  icon_url: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '图标地址'
  },
  category_id: {
    type: DataTypes.INTEGER,
    references: {
      model: LinkCategory,
      key: 'id',
    },
    allowNull: false,
    comment: '所属分类ID'
  },
  order: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '链接排序'
  },
  user_id: {
    type: DataTypes.INTEGER,
    references: {
      model: User,
      key: 'id',
    },
    allowNull: true,
    comment: '创建者ID'
  }
}, {
  freezeTableName: true
});


// Link.sync({ alter: true });
Link.associate = (models) => {
  Link.belongsTo(models.link_category, { foreignKey: 'category_id', as: 'linkGroup' });
  Link.belongsTo(models.blog_user, { foreignKey: 'user_id', as: 'creator' });
};
module.exports = Link;